﻿namespace Dcms.DAL
{
    using Dcms.Model;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;

    public class Dcms_FileFolderDAL
    {
        private string _fileorder = "";
        private string _sortstr = "";
        private string _WebsitePath = "";

        private string CheckFileName(string FileName, string FarmatPath)
        {
            if (FileName.Length > FarmatPath.Length) FileName = FileName.Substring(FarmatPath.Length, FileName.Length - FarmatPath.Length);
            FileName = FileName.Replace('\\', '/');
            return FileName;
        }

        private string CheckFilePath(string FileName)
        {
            if (FileName.Length > this._WebsitePath.Length) FileName = FileName.Substring(this._WebsitePath.Length, FileName.Length - this._WebsitePath.Length);
            FileName = FileName.Replace('\\', '/');
            return FileName;
        }

        public string GetCommFileInfo(string FilePath, string FileName, string FileExt, ref string FileType, ref string FileOptionStr)
        {
            string str = "";
            string str2 = "";
            switch (FileExt.ToLower())
            {
                case ".html":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/html.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "Html文件类型";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 1);
                    return str;

                case ".htm":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/html.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "Html文件类型";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 1);
                    return str;

                case ".aspx":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/aspx.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "aspx页面";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 1);
                    return str;

                case ".aspx.cs":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/cs.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "C语言源文件类型";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 2);
                    return str;

                case ".shtml":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/html.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "shtml";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 1);
                    return str;

                case ".cs":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/cs.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = ".net源文件类型";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 2);
                    return str;

                case ".asp":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/asp.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "asp页面";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 1);
                    return str;

                case ".php":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/php.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "Php文件类型";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 1);
                    return str;

                case ".jsp":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/jsp.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "jsp";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 1);
                    return str;

                case ".java":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/java.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "java";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 1);
                    return str;

                case ".css":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/css.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "样式文档";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 2);
                    return str;

                case ".xml":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/xml.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "xml文档";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 2);
                    return str;

                case ".vb":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/vb.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "Vb源文件类型";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 2);
                    return str;

                case ".js":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/js.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "脚本语言";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 2);
                    return str;

                case ".txt":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/txt.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "文本文件类型";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 2);
                    return str;

                case ".ascx":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/ascx.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "net用户控件";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 2);
                    return str;

                case ".ini":
                    str = "<img src=\"../../Images/file/ini.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName;
                    FileType = "ini";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 3);
                    return str;

                case ".log":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/log.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "日志文档";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 2);
                    return str;

                case ".inc":
                    str = FileName;
                    FileType = "inc";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 3);
                    return str;

                case ".config":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/config.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "。net配置文档";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 2);
                    return str;

                case ".cpp":
                    str = "<img src=\"../../Images/file/txt.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName;
                    FileType = "ppt文档";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 3);
                    return str;

                case ".ashx":
                    str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + this.CheckFilePath(FilePath) + "'";
                    str = "<a href=\"javascript:top.AlertMyMsg('系统提示信息', 3,'750', '500'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/ashx.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName + "</a>";
                    FileType = "ashx文件类型";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 2);
                    return str;

                case ".jpg":
                    str = "<img src=\"../../Images/file/jpg.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName;
                    FileType = "jpg图片";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 4);
                    return str;

                case ".gif":
                    str = "<img src=\"../../Images/file/gif.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName;
                    FileType = "gif图片";
                    FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 4);
                    return str;
            }
            str = "<img src=\"../../Images/file/txt.gif\" alt=\"文件类型\" align=\"absmiddle\" /> " + FileName;
            FileType = "未知文件类型类型";
            FileOptionStr = this.GetFileOptionStr(FilePath, FileName, 3);
            return str;
        }

        public List<Dcms_FileFolderModel> GetDirectories(string p_folderPath, string FileOrder, string SortStr, string FarmatPath, ref int FolderNum, ref int FileNum)
        {
            string name = "";
            string str2 = "";
            string fileType = "";
            List<Dcms_FileFolderModel> list = new List<Dcms_FileFolderModel>();
            this._WebsitePath = FarmatPath;
            this._fileorder = FileOrder;
            this._sortstr = SortStr;
            DirectoryInfo info3 = new DirectoryInfo(p_folderPath);
            try
            {
                foreach (FileSystemInfo info4 in info3.GetFileSystemInfos())
                {
                    if (info4 is DirectoryInfo)
                    {
                        DirectoryInfo info = info4 as DirectoryInfo;
                        string str4 = this.CheckFileName(info.FullName, FarmatPath);
                        string str5 = "";
                        if (str4.LastIndexOf('/') == -1)
                            str5 = "";
                        else
                            str5 = str4.Substring(0, str4.LastIndexOf('/'));
                        string str6 = "'Modle/Template/FileFolderEdit.aspx?Action=Edit&Type=folder&Path=" + str4 + "'";
                        name = "<a href=\"FileList.aspx?Path=" + str4 + "\"><img src=\"../../Images/file/IcoFolder.gif\" alt=\"文件类型夹\" align=\"absmiddle\" /> " + info.Name + "</a>";
                        str2 = "<a href=\"javascript:top.AlertMyMsg('Dcms系统提示信息', 3,'650', '250'," + str6 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/Rename.gif\" alt=\"重命名\" align=\"absmiddle\" /></a>&nbsp;&nbsp<a href=\"FileList.aspx?Type=folder&Path=" + str5 + "&Name=" + info.Name + "&Order=" + FileOrder + "&Sort=" + SortStr + "\" onclick=\"javascript:return confirm('你确认删除" + info.Name + " ?');\"><img src=\"../../images/file/Delete.gif\"  border=\"0px\"  alt=\"删除\" /></a>";
                        fileType = "文件类型夹";
                        Dcms_FileFolderModel item = new Dcms_FileFolderModel(info.Name, this.CheckFileName(info.FullName, FarmatPath), name, "", 0, "文件类型夹", info.LastWriteTime, str2);
                        list.Add(item);
                        FolderNum++;
                    }
                    else
                    {
                        FileInfo info2 = info4 as FileInfo;
                        name = info2.Name;
                        name = this.GetCommFileInfo(info2.FullName, info2.Name, info2.Extension, ref fileType, ref str2);
                        Dcms_FileFolderModel model2 = new Dcms_FileFolderModel(info2.Name, this.CheckFileName(info2.FullName, FarmatPath), name, info2.Extension, info2.Length, fileType, info2.LastWriteTime, str2);
                        list.Add(model2);
                        FileNum++;
                    }
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            if (!string.IsNullOrEmpty(FileOrder)) list.Sort(new Dcms_FilesComparer(FileOrder, SortStr));
            return list;
        }

        public string GetFileOptionStr(string FilePath, string FileName, int TypeFlag)
        {
            StringBuilder builder = new StringBuilder();
            string str = "";
            string str2 = "";
            string str3 = "";
            string str4 = "";
            string str5 = "";
            string str6 = "";
            str = this.CheckFilePath(FilePath);
            if (str.LastIndexOf('/') == -1)
                str6 = "";
            else
                str6 = str.Substring(0, str.LastIndexOf('/'));
            str2 = "'Modle/Template/FileEdit.aspx?Type=Edit&Path=" + str + "'";
            str4 = "/" + str;
            str3 = "'Modle/Template/FileFolderEdit.aspx?Action=Edit&Type=file&Path=" + str + "'";
            str5 = "<a href=\"FileList.aspx?Type=file&Path=" + str6 + "&Name=" + FileName + "&Order=" + this._fileorder + "&Sort=" + this._sortstr + "\" onclick=\"javascript:return confirm('你确认删除" + FileName + " ?');\"><img src=\"../../images/file/Delete.gif\"  border=\"0px\"  alt=\"删除\" /></a>";
            string str8 = TypeFlag.ToString();
            if (str8 != null)
            {
                if (!(str8 == "1"))
                {
                    if (str8 == "2")
                    {
                        builder.Append("<a href=\"javascript:top.AlertMyMsg('Dcms系统提示信息', 3,'650', '250'," + str3 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/Rename.gif\" alt=\"重命名\" align=\"absmiddle\" /></a>&nbsp;&nbsp;");
                        builder.Append("<a href=\"javascript:top.AlertMyMsg('Dcms系统提示信息', 3,'750', '475'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/Edit.gif\" alt=\"修改\" align=\"absmiddle\" /></a>&nbsp;&nbsp;");
                        builder.Append(str5);
                    }
                    else if (str8 == "3")
                    {
                        builder.Append("<a href=\"javascript:top.AlertMyMsg('Dcms系统提示信息', 3,'650', '250'," + str3 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/Rename.gif\" alt=\"重命名\" align=\"absmiddle\" /></a>&nbsp;&nbsp;");
                        builder.Append(str5);
                    }
                    else if (str8 == "4")
                    {
                        builder.Append("<a href=\"javascript:top.AlertMyMsg('Dcms系统提示信息', 3,'750', '250'," + str3 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/Rename.gif\" alt=\"重命名\" align=\"absmiddle\" /></a>&nbsp;&nbsp;");
                        builder.Append("<a href=\"" + str4 + "\" target=\"_blank\"><img src=\"../../Images/file/view.gif\" alt=\"预览\" align=\"absmiddle\" /></a>&nbsp;&nbsp;" + str5);
                    }
                }
                else
                {
                    builder.Append("<a href=\"javascript:top.AlertMyMsg('Dcms系统提示信息', 3,'650', '250'," + str3 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/Rename.gif\" alt=\"重命名\" align=\"absmiddle\" /></a>&nbsp;&nbsp;");
                    builder.Append("<a href=\"javascript:top.AlertMyMsg('Dcms系统提示信息', 3,'750', '475'," + str2 + ", window.location,'top.msg_close_tmp_biyuan(0)');\"><img src=\"../../Images/file/Edit.gif\" alt=\"修改\" align=\"absmiddle\" /></a>&nbsp;&nbsp;");
                    builder.Append("<a href=\"" + str4 + "\" target=\"_blank\"><img src=\"../../Images/file/view.gif\" alt=\"预览\" align=\"absmiddle\" /></a>&nbsp;&nbsp;" + str5);
                }
            }
            return builder.ToString();
        }
    }
}

